package com.anolesoft.amljmes.fortysix.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.amljmes.fortysix.dto.PtckSpDto;
import com.anolesoft.amljmes.fortysix.dto.MesBzzPtdmxDto;
import com.anolesoft.amljmes.fortysix.dto.PtckSpVo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;
/**
 * @author lx
 * @date 2022/11/25
 * 配套出库审批
*/
@Mapper
public interface PtckSpMapper {
    // 根据配套单主键查询配套单明细
    @Select("select a.* from mes_bzz_ptdmx a where a.matching_doc_pk = #{matchingDocPk, jdbcType=VARCHAR}")
    List<MesBzzPtdmxDto> queryMesBzzptdmx(@Param("matchingDocPk") String matchingDocPk);

    //将状态变成已驳回
    @Update("    update mes_bzz_ptd" +
            "      set state = 5" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateMesBzzBh(String objuid);



    //将状态变成待审批
    @Update("    update plan_goods_bind" +
            "      set state = 3" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateSp(String objuid);
    //将状态变成绑定
    @Update("    update plan_goods_bind" +
            "      set state = 0" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateCh(String objuid);
    //将状态变成通过
    @Update("    update plan_goods_bind" +
            "      set state = 4" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateTg(String objuid);
    //将状态变成提交驳回
    @Update("    update plan_goods_bind" +
            "      set state = 5" +
            "    where objuid = #{objuid, jdbcType=VARCHAR}"
    )
    int updateBh(String objuid);

    // 查询状态为绑定待审批
    @SelectProvider(type=PtckSpMapper.createSQL.class, method = "queryMesBzzTsSQL")
    List<PtckSpDto> queryMesBzzTsSQL(PtckSpVo demoVO);
    public static class createSQL {
        public static String queryMesBzzTsSQL(PtckSpVo demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid              as key,\n" +
                            "       a.supporting_order_no,\n" +
                            "       a.dispatch,\n" +
                            "       a.batch,\n" +
                            "       a.model,\n" +
                            "       a.sorties,\n" +
                            "       a.fathernumber,\n" +
                            "       a.fathername,\n" +
                            "       a.proportion,\n" +
                            "       a.danwei,\n" +
                            "       a.danjishu,\n" +
                            "       a.affiliation,\n" +
                            "       c.part_drawing_no drawing_no,\n" +
                            "       a.factory_code,\n" +
                            "       c.name,\n" +
                            "       c.jiaofuchejian,\n" +
                            "       c.jieshouchejian,\n" +
                            "       c.jiaofushunxu,\n" +
                            "       b.objuid,\n" +
                            "       b.hegebiaoqian,\n" +
                            "       b.bangdingnum,\n" +
                            "       z.name bangdingren,\n" +
                            "       b.bangdingtime,\n" +
                            "       b.xianghao,\n" +
                            "       y.name jianyanren,\n" +
                            "       b.jianyantime,\n" +
                            "       b.state,\n" +
                            "       0                     as pageNo,\n" +
                            "       10                    as pageSize\n" +
                            "  from mes_bzz_ptd a\n" +
                            "  left join mes_bzz_ptdmx c\n" +
                            "    on a.objuid = c.matching_doc_pk\n" +
                            "  left join plan_goods_bind b\n" +
                            "    on a.objuid = b.ptdmx_obj\n" +
                            "  left join stf_employee z\n" +
                            "    on b.bangdingren = z.objuid\n" +
                            "  left join stf_employee y\n" +
                            "    on b.jianyanren = y.objuid\n" +
                            " where b.state in(0,3)\n" +
                            "   and a.delete_tag = 1");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathernumber())){
                        String fathernumber = demoVO.getFathernumber();
                        sql.append(" and upper(a.fathernumber) = trim(upper(#{fathernumber, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSupportingOrderNo())){
                        String supportingOrderNo = demoVO.getSupportingOrderNo();
                        sql.append(" and upper(a.supporting_order_no) = trim(upper(#{supportingOrderNo, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getModel())){
                        String model = demoVO.getModel();
                        sql.append(" and upper(a.model) = trim(upper(#{model, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getBatch())){
                        String batch = demoVO.getBatch();
                        sql.append(" and upper(a.batch) = trim(upper(#{batch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSorties())){
                        String sorties = demoVO.getSorties();
                        sql.append(" and upper(a.sorties) = trim(upper(#{sorties, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getAffiliation())){
                        String affiliation = demoVO.getAffiliation();
                        sql.append(" and upper(a.affiliation) = trim(upper(#{affiliation, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactorycode())){
                        String factorycode = demoVO.getFactorycode();
                        sql.append(" and upper(a.factory_code) = upper(#{factorycode, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }

    // 查询状态为待审批
    @SelectProvider(type=PtckSpMapper.createSQL1.class, method = "queryMesBzzSpSQL")
    List<PtckSpDto> queryMesBzzSpSQL(PtckSpVo demoVO);
    public static class createSQL1 {
        public static String queryMesBzzSpSQL(PtckSpVo demoVO){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" a.objuid              as key,\n" +
                            "       a.supporting_order_no,\n" +
                            "       a.dispatch,\n" +
                            "       a.batch,\n" +
                            "       a.model,\n" +
                            "       a.sorties,\n" +
                            "       a.fathernumber,\n" +
                            "       a.fathername,\n" +
                            "       a.proportion,\n" +
                            "       a.danwei,\n" +
                            "       a.danjishu,\n" +
                            "       a.affiliation,\n" +
                            "       c.part_drawing_no drawing_no,\n" +
                            "       a.factory_code,\n" +
                            "       c.name,\n" +
                            "       c.jiaofuchejian,\n" +
                            "       c.jieshouchejian,\n" +
                            "       c.jiaofushunxu,\n" +
                            "       b.objuid,\n" +
                            "       b.hegebiaoqian,\n" +
                            "       b.bangdingnum,\n" +
                            "       z.name bangdingren,\n" +
                            "       b.bangdingtime,\n" +
                            "       b.xianghao,\n" +
                            "       y.name jianyanren,\n" +
                            "       b.jianyantime,\n" +
                            "       b.state,\n" +
                            "       0                     as pageNo,\n" +
                            "       10                    as pageSize\n" +
                            "  from mes_bzz_ptd a\n" +
                            "  left join mes_bzz_ptdmx c\n" +
                            "    on a.objuid = c.matching_doc_pk\n" +
                            "  left join plan_goods_bind b\n" +
                            "    on a.objuid = b.ptdmx_obj\n" +
                            "  left join stf_employee z\n" +
                            "    on b.bangdingren = z.objuid\n" +
                            "  left join stf_employee y\n" +
                            "    on b.jianyanren = y.objuid\n" +
                            " where b.state = 3\n" +
                            "   and a.delete_tag = 1");
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFathernumber())){
                        String fathernumber = demoVO.getFathernumber();
                        sql.append(" and upper(a.fathernumber) = trim(upper(#{fathernumber, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSupportingOrderNo())){
                        String supportingOrderNo = demoVO.getSupportingOrderNo();
                        sql.append(" and upper(a.supporting_order_no) = trim(upper(#{supportingOrderNo, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getModel())){
                        String model = demoVO.getModel();
                        sql.append(" and upper(a.model) = trim(upper(#{model, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getBatch())){
                        String batch = demoVO.getBatch();
                        sql.append(" and upper(a.batch) = trim(upper(#{batch, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getSorties())){
                        String sorties = demoVO.getSorties();
                        sql.append(" and upper(a.sorties) = trim(upper(#{sorties, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getAffiliation())){
                        String affiliation = demoVO.getAffiliation();
                        sql.append(" and upper(a.affiliation) = trim(upper(#{affiliation, jdbcType=VARCHAR}))");
                    }
                    if(!StrUtil.isEmptyOrUndefined(demoVO.getFactorycode())){
                        String factorycode = demoVO.getFactorycode();
                        sql.append(" and upper(a.factory_code) = upper(#{factorycode, jdbcType=VARCHAR})");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }
}
